<?php
/**
 * @title 167. 两数之和 II - 输入有序数组 - 哈希
 * @author start2004
 */

class Solution {

    /**
     * @param Integer[] $numbers
     * @param Integer $target
     * @return Integer[]
     */
    function twoSum($numbers, $target) {
        /**
         * @since 2020-09-08 哈希
         */
        $hashs = [];
        for($i=0, $iMax=count($numbers)-1; $i<=$iMax; $i++){
            $hashs[$numbers[$i]][] = $i;
        }

        /**
         * @since 2020-09-08 找到目标求和值
         */
        foreach ($hashs as $num=>$keys){
            $index1 = $keys[0];
            $num2 = $target-$num;

            if(isset($hashs[$num2])){
                /**
                 * @since 2020-09-08 不能重复
                 */
                foreach ($hashs[$num2] as $k){
                    if($k !== $index1){
                        return [$index1+1, $k+1];
                        break;
                    } else {}
                }
            } else {}
        }

        /**
         * @return
         */
        return false;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.20.php
 */
$datas = [
    [
        [2, 7, 11, 15], 9
    ]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->twoSum($data[0], $data[1]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
